//package com.android.hqview;
//
//import android.content.Context;
//import android.graphics.Color;
//import android.os.Bundle;
//import android.text.Html;
//import android.text.TextUtils;
//import android.util.SparseArray;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.widget.FrameLayout;
//import android.widget.TextView;
//
//import com.galaxy.internet.galaxystock.R;
//import com.galaxy.internet.galaxystock.bean.BusUtils;
//import com.galaxy.internet.galaxystock.bean.QuoteFiveMinutesData;
//import com.galaxy.internet.galaxystock.common.CommonType;
//import com.galaxy.internet.galaxystock.ui.activity.common.GalaxyMainActivity;
//import com.galaxy.internet.galaxystock.utils.DateUtils;
//import com.galaxy.internet.galaxystock.utils.PriceColorUtil;
//import com.galaxy.internet.galaxystock.utils.StringUtils;
//import com.quote.app.android.contract.HqFiveDayContract;
//import com.quote.app.android.presenters.HqFiveDayPresenter;
//import com.quote.mp.chart.charts.MinutesChart;
//import com.quote.mp.chart.data.Entry;
//import com.quote.mp.chart.data.MinutesData;
//import com.quote.mp.chart.formatter.ChengJiaoLiangFormatter;
//import com.quote.mp.chart.formatter.DataFormatter;
//import com.quote.mp.chart.formatter.YValueFormatter;
//import com.quote.mp.chart.listener.OnMarketSelectValueListener;
//import com.quote.mp.chart.markerview.HightCircleMarkerView;
//import com.quote.mp.chart.markerview.LeftMarkerView;
//import com.quote.mp.chart.markerview.QuoteInfoMarkerView;
//import com.quote.mp.chart.markerview.RightMarkerView;
//import com.quote.mp.chart.markerview.XAxisMarkerView;
//import com.quote.mp.chart.view.ShinePointView;
//
//import org.greenrobot.eventbus.EventBus;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import static com.quote.mp.chart.components.AJXAxis.TextPosition_Middle;
//import static com.quote.mp.chart.components.AJXAxis.XAXISPOSITION_MIDDLE;
//
///**
// * 五日view
// * Created by TenFace on 17/10/25.
// */
//
//public class FiveDayView extends BaseChartView implements HqFiveDayContract.View, OnMarketSelectValueListener {
//    private MinutesChart mMinutesChart;
//    private HqFiveDayPresenter presenter;
//    private float lastClose;
//    private float maxmin;
//    private int count = 1;
//    private FrameLayout chartTopHigh;
//    private float volmax;
//    private SparseArray<String> dayLabels = new SparseArray<>();
//    private View v;
//    private boolean isFullScreen;
//    private TextView highTime, highOpen, highHigh, highLow, highClose, highFu;
//    private String upColor, downColor, equalColor, tvColor;
//    private ArrayList<MinutesData> minutesDatas;
//    private boolean isZS = false;
//
//    public FiveDayView(Context context, FrameLayout v, Bundle bundle) {
//        super(context, bundle);
//        this.chartTopHigh = v;
//    }
//
//    public FiveDayView(Context context, Bundle bundle) {
//        super(context, bundle);
//    }
//
//    @Override
//    public int getLayoutId() {
//        return R.layout.chart_fiveday;
//    }
//
//    @Override
//    public void initLayout(Context context) {
//        mMinutesChart = (MinutesChart) findViewById(R.id.fiveDayChart);
//        initChart();
//        initTabHighLightView();
//    }
//
//    @Override
//    public void initData() {
//        String type = mBundle.getString("type");
//        String name = mBundle.getString("name");
//        isFullScreen = mBundle.getBoolean("fullScreen");
//        presenter = new HqFiveDayPresenter(this, "000005", "1");
//        presenter.getFiveDayInfo();
//    }
//
//    private void initTabHighLightView() {
//        v = LayoutInflater.from(getContext()).inflate(R.layout.hq_highlight_layout, null);
//        highTime = (TextView) v.findViewById(R.id.highTime);
//        highOpen = (TextView) v.findViewById(R.id.highOpen);
//        highHigh = (TextView) v.findViewById(R.id.highHigh);
//        highClose = (TextView) v.findViewById(R.id.highClose);
//        highLow = (TextView) v.findViewById(R.id.highLow);
//        highFu = (TextView) v.findViewById(R.id.highFu);
//    }
//
//    private void initChart() {
//        upColor = DataFormatter.colorToString(getResources().getColor(R.color.chartRed));
//        downColor = DataFormatter.colorToString(getResources().getColor(R.color.chartGreen));
//        equalColor = DataFormatter.colorToString(getResources().getColor(R.color.mainFontColor));
//        tvColor = DataFormatter.colorToString(getResources().getColor(R.color.mainFontColor));
//        mMinutesChart.setXAxisLabelColor(getResources().getColor(R.color.mainFontColor));
//        mMinutesChart.setGridLineColor(Color.TRANSPARENT);
//        mMinutesChart.setHighLightColor(getResources().getColor(R.color.highlight));
//        mMinutesChart.setYLabelColors(getResources().getColor(R.color.chartGreen), getResources().getColor(R.color.chartRed));
//        mMinutesChart.setAxisLeftValueFormatter(new YValueFormatter("#0.00"));
//        mMinutesChart.setAxisRightValueFormatter(new YValueFormatter("#0.00"));
//        mMinutesChart.setDrawBorder(Color.argb(255, 17, 16, 19), 1);
//        mMinutesChart.setLabelCount(2);
//        mMinutesChart.setYAxisOffset(2);
//        mMinutesChart.setYLabelStatic(true);
//        mMinutesChart.setLRMarkerView(
//                new QuoteInfoMarkerView("timeline", getContext(), R.layout.quote_info_markerview, false),
//                new LeftMarkerView("timeline", getContext(), R.layout.share_marker_view, false, "#0.00"),
//                new RightMarkerView("timeline", getContext(), R.layout.share_marker_view, false, "#0.00"),
//                new XAxisMarkerView(getContext(), R.layout.share_marker_view, "minutes"));//new XAxisMarkerView(getContext(), R.layout.share_marker_view)
//        mMinutesChart.setHighLightCircle(new HightCircleMarkerView(getContext(), getResources().getColor(R.color.highlight)));
//        mMinutesChart.setShinePointView(new ShinePointView(getContext(), getResources().getColor(R.color.minuteLineColor)));
//        mMinutesChart.setXLabelPosition(XAXISPOSITION_MIDDLE);
//        mMinutesChart.setXAxisTextPosition(TextPosition_Middle);
//        mMinutesChart.setDrawLinePathFillEnabled(false);
//        mMinutesChart.setOnChartGestureListener(this);
//        mMinutesChart.setOnMarketSelectValueListener(this);
//        isZS = GalaxyMainActivity.userPreferences.getBoolean(CommonType.QUOTE_MINUTES_IS_ZHI_SHU, false);
//    }
//
//    @Override
//    public void refreshData() {
//        presenter.getFiveDayInfo();
//    }
//
//    @Override
//    public void showToast(String msg) {
//
//    }
//
//    @Override
//    public void networkError() {
//
//    }
//
//    @Override
//    public void onValueSelected(int xIndex, Entry klineEntry, Entry affilateEntry) {
//        mMinutesChart.requestDisallowInterceptTouchEvent(true);
//        EventBus.getDefault().post(new BusUtils.refreshStatus(false));
//
//        if (isFullScreen) {
//            MinutesData kData = (MinutesData) klineEntry.getData();
//            float baseClose;
//            if (xIndex == 0) {
//                baseClose = kData.lastClose;
//            } else {
//                baseClose = kData.lastClose;
//            }
//            String color = kData.chengjiaojia > baseClose ? upColor : (kData.chengjiaojia == baseClose ? equalColor : downColor);
//
//
//            highTime.setText(kData.time);
//
//            highOpen.setText(Html.fromHtml(String.format("<font color='%s'>价格</font>&nbsp;<font color='%s'>%s</font>", tvColor, color, StringUtils.doubleToDoubleString(kData.chengjiaojia))));
//
//            highHigh.setText("涨跌" + String.valueOf(kData.percentage));
//            PriceColorUtil.setPercentColor(getContext(),highHigh, kData.percentage);
//
//            highLow.setText("成交量" + String.valueOf(kData.chengjiaoliang));
//            color = kData.chengjiaojia > baseClose ? upColor : (kData.chengjiaojia == baseClose ? equalColor : downColor);
//            highClose.setText(Html.fromHtml(String.format("<font color='%s'>成交均价</font>&nbsp;<font color='%s'>%s</font>", tvColor, color, StringUtils.doubleToDoubleString(kData.junjia))));
//            highFu.setVisibility(GONE);
//            //  LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//            if (chartTopHigh != null) {
//                chartTopHigh.removeAllViews();
//            }
//            chartTopHigh.addView(v);
//        }
//
//    }
//
//    @Override
//    public void onValueNothing() {
//        mMinutesChart.requestDisallowInterceptTouchEvent(false);
//        EventBus.getDefault().post(new BusUtils.refreshStatus(true));
//        if (isFullScreen) {
//            chartTopHigh.removeAllViews();
////            Log.e("touch", "onValueNothing");
//        }
//    }
//
//    private String doDate(String date) {
//        if (TextUtils.isEmpty(date) || date.length() < 8) {
//            return "";
//        }
//
//        return date.substring(4, 6) + "-" + date.substring(6);
//    }
//
//    public String[] getMinutesCount() {
//        return new String[490];
//    }
//
//    @Override
//    public void updateHqInfo(List<QuoteFiveMinutesData.DataBean.MinBean> infos) {
//        stop();
//        dayLabels = setXLabels(infos);
//        minutesDatas = new ArrayList<>();
//        float max = (float) infos.get(0).getCurrent();
//        float min = (float) infos.get(0).getCurrent();
//        lastClose = (float) infos.get(0).getLast_close();//昨收
//        float maxPercent = (float) (infos.get(0).getPercent());
//        float minPercent = (float) (infos.get(0).getPercent());
//
//        int total = infos.size();
//        List<QuoteFiveMinutesData.DataBean.MinBean> minuteList = infos;
//        for (int i = 0; i < total; i++) {
//            MinutesData minutesData = new MinutesData();
//            float currentPrice = (float) minuteList.get(i).getCurrent();
//            float percent = (float) (minuteList.get(i).getPercent());
//            minutesData.chengjiaojia = currentPrice;
//            minutesData.chengjiaoliang = (float) minuteList.get(i).getVolume();
//            minutesData.percentage = (float) (minuteList.get(i).getPercent());
//            minutesData.junjia = (float) minuteList.get(i).getAvg();
////            minutesData.time = Long.parseLong(minuteList.get(i).getTime());
//            minutesData.time = DateUtils.getDateToStringHm(minuteList.get(i).getTime());
//            if (currentPrice > max) {
//                max = currentPrice;
//            }
//            if (currentPrice < min) {
//                min = currentPrice;
//            }
//
//            if (percent > maxPercent) {
//                maxPercent = percent;
//            }
//            if (percent < minPercent) {
//                minPercent = percent;
//            }
//            volmax = Math.max(minutesData.chengjiaoliang, volmax);
//            minutesDatas.add(minutesData);
//        }
//
//        if (lastClose > max) {
//            max = lastClose;
//        }
//
//        if (lastClose < min) {
//            min = lastClose;
//        }
//
//        if (maxmin == 0) {
//            maxmin = lastClose * 0.02f;
//        }
//        String jishu = DataFormatter.getVolJishu(volmax);
//        int js = 1;
//
//        if (jishu.equals("万股")) {
//            js = 4;
//        } else if (jishu.equals("亿股")) {
//            js = 8;
//        }
//        mMinutesChart.setShowLabels(dayLabels);
//        mMinutesChart.setBarValueFormatter(new ChengJiaoLiangFormatter((int) Math.pow(10, js)));
//        mMinutesChart.setBarMaxText(jishu);  //柱状图只设置最大值
//        mMinutesChart.setMaxMin(max, min, maxPercent / 100, minPercent / 100, 5);//必须放在前面
//        mMinutesChart.setBaseLine(lastClose);//设置的是折线图基准线
//        mMinutesChart.setData(getMinutesCount(), minutesDatas, 1, isZS);
//        mMinutesChart.invalidate();
//
//        dispatchTimerHandler();
//    }
//
//    private SparseArray<String> setXLabels(List<QuoteFiveMinutesData.DataBean.MinBean> infos) {
//        SparseArray<String> xLabels = new SparseArray<>();
//        xLabels.put(49, DateUtils.getDateToStringMD(infos.get(49).getTime()));
//        xLabels.put(147, DateUtils.getDateToStringMD(infos.get(147).getTime()));
//        xLabels.put(245, DateUtils.getDateToStringMD(infos.get(245).getTime()));
//        xLabels.put(343, DateUtils.getDateToStringMD(infos.get(343).getTime()));
//        xLabels.put(441, DateUtils.getDateToStringMD(infos.get(393).getTime()));
//        return xLabels;
//    }
//
//   /* @Override//理论快 实际慢 后期优化
//    public void updateHqFiveDayInfo(List<HqFiveDayInfo> infos) {
//        ArrayList<ArrayList<MinutesData>> minuteList = new ArrayList<>();
//        lastClose = Float.parseFloat(infos.get(0).lastclose);
//        float max = infos.get(0).timeLineItemList.get(0).current;
//        float min = infos.get(0).timeLineItemList.get(0).current;
//        int currentCount = 0;
//        ArrayList<Entry> chengjiaojiaList = new ArrayList<>();
//        List<Integer> xlabelIndexs = new ArrayList<>();
//        List<Long> xlabelValue = new ArrayList<>();
//        float lastCurrentP = 0;//非当日最后数据补齐
//        for (int i = infos.size() - 1; i >= 0; i--) {
//            ArrayList<MinutesData> minutesDatas = new ArrayList<>();
//            List<HqFiveDayInfo.TimeLineItemListBean> timeLineItemList = infos.get(i).timeLineItemList;
//            xlabelIndexs.add(currentCount);
//            int prei = 0;
//            long durationTime = TimeUtils.getTimePTHM(infos.get(i).duration);
//            int count = (int) durationTime / 60000 + 1;
//            long openTime = infos.get(i).openTime;
//            xlabelValue.add(openTime);
//            MinutesData minutesData = new MinutesData();
//            for (int j = 0; j < count; j++, openTime += 60000) {
//                if (prei < timeLineItemList.size()) {//网络指针prei小于网络数据个数
//                    float currentPrice = timeLineItemList.get(prei).current;
//              *//*      minutesData.chengjiaojia = currentPrice;
//                    minutesData.percentage = ((currentPrice - lastClose) / lastClose);
//                    minutesData.time = openTime;*//*
//                    if (currentPrice > max) {
//                        max = currentPrice;
//                    }
//                    if (currentPrice < min) {
//                        min = currentPrice;
//                    }
//                    double cha = currentPrice - lastClose;
//                    if (Math.abs(cha) > maxmin) {
//                        maxmin = (float) Math.abs(cha);
//                    }
//                    minutesData.time=openTime;
//                    minutesData.chengjiaojia=currentPrice;
//                    // minutesData = new MinutesData(openTime, timeLineItemList.get(prei).current);
//                    if(timeLineItemList.get(prei).time<=openTime){
//                        prei++;
//                    }
//                    chengjiaojiaList.add(new Entry(currentPrice, j + currentCount, minutesData));//封装表数据类型,x值=当日i索引+之前日count
//                } else {
//                    if (i != 0) {//非今日最后无数据情况下取那日最后一条
//                        lastCurrentP = minutesData.chengjiaojia;
//                        minutesData.time=openTime;
//                        minutesData.chengjiaojia=lastCurrentP;
//                        //MinutesData t2 = new MinutesData(openTime, lastCurrentP);
//                        chengjiaojiaList.add(new Entry(lastCurrentP, j + currentCount, minutesData));
//                    } else {//当日最后数据不处理
//                        break;
//                    }
//                }
//            }
//            currentCount += count;
//            minuteList.add(minutesDatas);
//        }
//        Log.e("tenface", "网络请求后第三次遍历-->" + DateUtil.getTimestamp());//返回结果精确到毫秒。
//        if (maxmin == 0) {
//            maxmin = lastClose * 0.02f;
//        }
//        mMinutesChart.setMaxMin(lastClose + maxmin, lastClose - maxmin, maxmin / lastClose, -maxmin / lastClose, 5);//必须放在前面
//        mMinutesChart.setBaseLine(lastClose);//设置的是折线图基准线
//        mMinutesChart.setFiveData(chengjiaojiaList,xlabelIndexs,xlabelValue,currentCount);
//        //mMinutesChart.setFiveData(infos, minuteList);
//        mMinutesChart.invalidate();
//    }*/
//}
